package hnlg.com.cn.purviewmanage.usermanage.control;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import hnlg.com.cn.common.RespBean;
import hnlg.com.cn.model.RoleVO;
import hnlg.com.cn.model.UserVO;
import hnlg.com.cn.purviewmanage.rolemanage.service.IRoleService;
import hnlg.com.cn.purviewmanage.usermanage.service.IUserService;

@RestController
@RequestMapping("/UserManageControl")
public class UsermanageControl {
List<UserVO> list=null;
RespBean rb=null;
UserVO vo=null;
@Resource IUserService UserService;
@Resource IRoleService RoleService;
//http://localhost:8044/test/UserManageControl/queryList?
@RequestMapping("/queryList")
 public List<UserVO> queryList(
		@RequestParam(required=false,defaultValue="") String username,
		@RequestParam(required=false,defaultValue="") String pwd)throws Exception{
	try{
		UserVO vo=new UserVO();
		vo.setUsername(username);;vo.setPwd(pwd);
		list=UserService.queryList(vo);
	}catch(Exception e){
		e.printStackTrace();
	}
	return list;
}

@RequestMapping("/queryvo")
public UserVO queryvo(String id){
	try{
	System.out.println("id="+id);
	vo=UserService.queryvo(id);
	}catch(Exception e){
		e.printStackTrace();
	}
	return vo;
}

@RequestMapping("/adduser")
public RespBean adduser(UserVO vo){
	try{
	UserService.adduser(vo);
	rb=new RespBean("msg", "添加成功");	
	}catch(Exception e){
		rb=new RespBean("msg","添加失败");
		e.printStackTrace();
	}
	return rb;
}

@RequestMapping("/updateuser")
public RespBean updateuser(UserVO vo){
	try{
		System.out.println("id="+vo.getUserid());
		boolean flag=UserService.updateuser(vo);
		System.out.println("flag="+flag);
		rb=new RespBean("msg", "更新成功");
	}catch(Exception e){
		rb=new RespBean("msg", "更新失败");
		e.printStackTrace();
	}
	return rb;
}

@RequestMapping("/deleteuser")
public RespBean deleteuser(@RequestParam("userid") String userid){
	try{
		System.out.println("userid="+userid);
		UserService.deleteuser(userid);
		rb=new RespBean("msg", "删除成功");
	}catch(Exception e){
		rb=new RespBean("msg", "删除失败");
		e.printStackTrace();
	}
	return rb;
}

/* 去授权页面的处理：角色显示列表数据 */
@RequestMapping("/getRolelist")
public List<RoleVO> getRolelist(@RequestParam(
	required=false,defaultValue="")String userid)throws Exception{
	List<RoleVO> list=null;
	try{
	list=RoleService.queryList(new RoleVO());
	}catch(Exception e){
		e.printStackTrace();
	}
	return list;
	
}

/*用户授权角色*/
@RequestMapping("/useraddRole")
public RespBean useraddRole(
  @RequestParam(required=false,defaultValue="")String userid,
  @RequestParam(required=false,defaultValue="")String rolecheckbox[])throws Exception{
	try{
	long userid1=Long.parseLong(userid);	
	/*这里要做特需的处理：当授权选择了checkbox时按下面执行：先删除再加入*/
	/*如果没有选择checkbox:代表删除权限：此时的处理应该只有删除用户授权的角色，不需要增加*/	
	/*没有值去增加：mybatis会报错*/
	if(null!=rolecheckbox&&rolecheckbox.length>0){
		this.UserService.useraddRole(userid1, rolecheckbox);
	}
	else{
		this.UserService.deleteUserRole(userid1);
	}
	 rb=new RespBean("msg", "授权成功");
	}catch(Exception e){
		rb=new RespBean("msg", "授权失败");
		e.printStackTrace();
	}
	return rb;
}

/*查询那些角色已经受了权的角色id*/
@RequestMapping("/getExistRoleId")
public List<String> getExistRoleId(
	@RequestParam(required=false,defaultValue="")String userid){
	List<String> rolelist=null;
	try{
		/*根据用户id查询中间表：user_role表，该用户已经授权的角色id列表*/
		rolelist=this.UserService.queryRoleIdList(userid);
	}catch(Exception e){
		e.printStackTrace();
		
	}
	return rolelist;	
}



}
